package bcc.java8.chap05;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class PuttingIntoPractice {
    public static void main(String ...args){    
        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario","Milan");
        Trader alan = new Trader("Alan","Cambridge");
        Trader brian = new Trader("Brian","Cambridge");
		
		List<Transaction> transactions = Arrays.asList(
            new Transaction(brian, 2011, 300),
            new Transaction(raoul, 2012, 1000),
            new Transaction(raoul, 2011, 400),
            new Transaction(mario, 2012, 710),
            new Transaction(mario, 2012, 700),
            new Transaction(alan, 2012, 950)
        );
        List<Transaction> answer01 = transactions.stream().
                filter(item -> item.getYear() == 2011).
                sorted(Comparator.comparing(Transaction::getValue)).
                collect(Collectors.toList());
        System.out.println(answer01);
        List<Transaction> collect = transactions.stream()
                .filter(transaction -> transaction.getYear() == 2011)
                .sorted(Comparator.comparing(Transaction::getValue))
                .collect(Collectors.toList());

        List<String> answer02 = transactions.stream().map(item -> item.getTrader()).map(item -> item.getCity()).distinct().collect(Collectors.toList());
        System.out.println(answer02);

        transactions.stream().map(item -> item.getTrader().getCity())
                .distinct().collect(Collectors.toList());
        transactions.stream().map(item->item.getTrader().getCity()).collect(Collectors.toSet());


        List<Trader> answer03 = transactions.stream()
                .map(Transaction::getTrader)
                .filter(item -> item.getCity().equals("Cambridge"))
                .sorted(Comparator.comparing(Trader::getName))
                .collect(Collectors.toList());
        System.out.println(answer03);

        List<String> answer04 = transactions.stream().map(Transaction::getTrader).map(Trader::getName).distinct().sorted().collect(Collectors.toList());
        System.out.println(answer04);

        String reduce = transactions.stream().map(transaction -> transaction.getTrader().getName())
                .distinct()
                .sorted()
                .reduce("", (a, b) -> a + b);
        System.out.println(reduce);

        String collect1 = transactions.stream().map(transaction -> transaction.getTrader().getName())
                .distinct()
                .sorted()
                .collect(Collectors.joining());
        System.out.println(collect1);

        boolean answer05 = transactions.stream().map(Transaction::getTrader).anyMatch(item -> item.getCity().equals("Milan"));
        System.out.println(answer05);

        boolean milan = transactions.stream().anyMatch(item -> item.getTrader().getCity().equals("Milan"));
        System.out.println(milan);

        transactions.stream().filter(item -> item.getTrader().getCity().equals("Cambridge")).map(Transaction::getValue).forEach(System.out::println);

        // 第七题
        Optional<Integer> max = transactions.stream().filter(item -> item.getTrader().getCity().equals("Cambridge")).map(Transaction::getValue).max(Integer::compareTo);
        System.out.println(max.get());

        Optional<Integer> max1 = transactions.stream().map(item -> item.getValue()).max(Integer::compareTo);
        System.out.println(max1.get());
        Optional<Integer> reduce1 = transactions.stream().map(Transaction::getValue).reduce(Integer::max);

        //第八题
        Optional<Transaction> first = transactions.stream().sorted((a, b) -> a.getValue() - a.getValue()).findFirst();
        System.out.println(first.get());

        Optional<Transaction> reduce2 = transactions.stream().reduce((t1, t2) -> t1.getValue() < t2.getValue() ? t1 : t2);
        System.out.println(reduce2.get());

        Optional<Transaction> min = transactions.stream().min(Comparator.comparing(Transaction::getValue));
        System.out.println(min.get());

    }
}
